package com.smallcc.flesh.ui.activity

import android.content.res.Resources
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.preference.PreferenceManager
import android.support.v4.widget.DrawerLayout
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.View
import android.view.WindowManager

import com.bumptech.glide.Glide
import com.smallcc.flesh.Constants
import com.smallcc.flesh.R
import com.smallcc.flesh.presenter.MainActivityDelegate
import com.smallcc.netcore.network.AsyncNetwork
import com.smallcc.netcore.network.IRequestCallback

import org.json.JSONException
import org.json.JSONObject

import java.lang.reflect.Field
import java.net.HttpURLConnection

class MainActivity : AppCompatActivity() {

    private var mDelegate: MainActivityDelegate? = null

    protected val statusBarHeight: Int
        get() {
            val resources = resources
            val resourceId = resources.getIdentifier(STATUS_BAR_HEIGHT, "dimen", "android")
            return resources.getDimensionPixelSize(resourceId)
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        transparent()
        setContentView(R.layout.activity_main)

        val toolbar = findViewById(R.id.tool_bar) as Toolbar
        val drawerLayout = findViewById(R.id.drawer_layout) as DrawerLayout
        val drawerToggle = ActionBarDrawerToggle(this, drawerLayout, toolbar, 0, 0)
        drawerToggle.syncState()
        drawerLayout.setDrawerListener(drawerToggle)

        val content = findViewById(R.id.content)
        content.setPadding(content.paddingLeft, content.paddingTop + statusBarHeight, content.paddingRight, content.paddingBottom)

        checkZero()

        if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.PREF_ZERO, false)) {
            mDelegate = MainActivityDelegate(this)
        }

    }

    private fun checkZero() {
        val request = AsyncNetwork()
        request.setDoInputOutput(true, false)
        request.request(Constants.CONFIG_URL)
        request.setRequestCallback(object : IRequestCallback {
            override fun onSuccess(httpURLConnection: HttpURLConnection?, response: String) {
                try {
                    val json = JSONObject(response)
                    val zero = json.getBoolean("zero")
                    PreferenceManager.getDefaultSharedPreferences(this@MainActivity).edit()
                            .putBoolean(Constants.PREF_ZERO, zero)
                            .putString(Constants.PREF_NOTIFICATION_URL, json.getString("notification"))
                            .apply()
                    if (!zero && mDelegate == null) {
                        this@MainActivity.runOnUiThread {
                            if (mDelegate == null) {
                                mDelegate = MainActivityDelegate(this@MainActivity)
                            }
                        }
                    }
                } catch (e: JSONException) {
                }

            }
        })
    }

    override fun onStop() {
        super.onStop()
        if (mDelegate != null) {
            mDelegate!!.onStop()
        }
    }

    override fun onResume() {
        super.onResume()
        if (mDelegate != null) {
            mDelegate!!.onResume()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mDelegate != null) {
            mDelegate!!.onDestroy()
        }
        Glide.get(this).clearMemory()
    }

    protected fun transparent() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)  //去除半透明状态栏
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN //一般配合fitsSystemWindows()使用, 或者在根部局加上属性android:fitsSystemWindows="true", 使根部局全屏显示
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.statusBarColor = Color.TRANSPARENT
        }

        if (Build.VERSION.SDK_INT >= 24/*Build.VERSION_CODES.N*/) {
            try {
                val decorViewClazz = Class.forName("com.android.internal.policy.DecorView")
                val field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor")
                field.isAccessible = true
                field.setInt(window.decorView, Color.TRANSPARENT) //改为透明
            } catch (e: Exception) {
            }

        }
    }

    companion object {

        private val STATUS_BAR_HEIGHT = "status_bar_height"
    }
}
